package com.design.blog.utils;

import com.alibaba.fastjson.JSON;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

@Component
public class QiniuUtils {
    public static  final String url = "http://rb3w1xelj.hd-bkt.clouddn.com/";

    private static String accessKey="s1UNdbsRL2Ujp_LWlwoEI3p-y6Zd1TBUkbmgpX_J";
    private static String accessSecretKey = "q9uiQrpLZr9jeZbIfH_mcROlJ6tQPTdPi9IlJuEP";

    public static boolean upload(MultipartFile file,String fileName){
        Configuration cfg = new Configuration(Region.huadong());
        UploadManager uploadManager = new UploadManager(cfg);
        String bucket = "hqiniu";
        try {
            byte[] uploadBytes = file.getBytes();
            Auth auth = Auth.create(accessKey, accessSecretKey);
            String upToken = auth.uploadToken(bucket);
                Response response = uploadManager.put(uploadBytes, fileName, upToken);
                DefaultPutRet putRet = JSON.parseObject(response.bodyString(), DefaultPutRet.class);
                return true;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        return false;
    }
}
